/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.menu;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
import rpg.RPG;
import rpg.gfx.Bitmap;
import rpg.gfx.HSVGradientColor;
import rpg.ui.components.Component;
import rpg.ui.components.ComponentImageButton;
import rpg.gfx.dialogs.Dialog;
import rpg.gfx.dialogs.DialogFactory;
import rpg.gfx.menu.handlers.IMenuHandler;
import rpg.gfx.menu.handlers.SelectMenuHandler;
import rpg.mouse.MouseEventType;
import rpg.util.ImageUtil;

/**
 *
 * @author rothens
 */
public class MainMenu extends Bitmap implements IMenu {

    int clicks = 0;
    int cticks = 0;
    int poletop = 0;
    Bitmap sky;
    Bitmap[] btn = {
        ImageUtil.loadBitmap("/res/menu_on.png"),
        ImageUtil.loadBitmap("/res/menu_off.png")
    };
    private ArrayList<Component> components = new ArrayList<Component>();
    private int active;
    Random r = new Random();
    private Bitmap cloudimg[] = {
        ImageUtil.loadBitmapScaled("/res/cloud.png", 0.3),
        ImageUtil.loadBitmapScaled("/res/cloud.png", 0.5),
        ImageUtil.loadBitmapScaled("/res/cloud.png", 0.8),
        ImageUtil.loadBitmap("/res/cloud.png"),
        ImageUtil.loadBitmap("/res/menugrass.png"),
        ImageUtil.loadBitmap("/res/menumnt.png")
    };
    private Bitmap poleimg = ImageUtil.loadBitmap("/res/pole.png");
    private LinkedList<Cloud> clouds = new LinkedList<Cloud>();
    private Bitmap pole;
    private IMenuHandler handler = new SelectMenuHandler();

    public MainMenu(int width, int height) {
        super(width, height);
        createGradient(72, 10);
        init();
    }

    public void createGradient(int fromS, int toS) {
        sky = new Bitmap(width, height);
        HSVGradientColor hgc = new HSVGradientColor(HSVGradientColor.GradientType.S, 203, fromS, 98, toS, false);
        sky = hgc.createGradient(sky);
    }

    public void init() {
        clearWith(0x87CEFA);
        createCloud(0, 1, 2, 5);
        createCloud(1, 2, 4, 4);
        createCloud(2, 3, 5, 3);
        createCloud(3, 4, 7, 3);
        int[] color = {0xffff00};
        components.add(new ComponentImageButton(null, null, btn, "új játék", (byte) 2, color) {
            @Override
            public void action() {
                RPG.rpg.newGame();
            }
        });

        components.add(new ComponentImageButton(null, null, btn, "kilépés", (byte) 2, color) {
            @Override
            public void action() {
                System.exit(0);
            }
        });
        handler.setComponents(components);
        active = 0;
        generateMenus(5);
    }

    public void generateMenus(int spacing) {
        int h = components.size() * btn[0].height + (components.size() - 1) * spacing;
        int y = RPG.WINDOW_HEIGHT - 40 - (h);
        for (Component c : components) {
            c.setY(y);
            y += btn[0].height + spacing;
        }
        pole = new Bitmap(12, h + 45);
        poletop = RPG.WINDOW_HEIGHT - h - 45;
        for (int i = 0; i < pole.height; i += poleimg.height) {
            pole.draw(poleimg, 0, i);
        }
    }

    public void createCloud(int id, int minspeed, int maxspeed, int amt) {
        for (int i = 0; i < amt; i++) {
            clouds.add(new Cloud(cloudimg[id].width, r.nextInt(RPG.WINDOW_WIDTH), r.nextInt(RPG.WINDOW_HEIGHT - 130 - cloudimg[id].height), minspeed + r.nextInt(maxspeed - minspeed), id));
        }
    }

    @Override
    public void tick() {
        if (clicks > 0) {
            cticks++;
            if (cticks > 10) {
                cticks = 0;
                clicks = 0;
            }
        }
        for (Cloud c : clouds) {
            c.tick();
        }

        if (handler.handleTopEvents()) {
            return;
        }

        if (RPG.keylistener.isKeyDown(KeyEvent.VK_ESCAPE)) {
            System.exit(0);
            return;
        }

        if (RPG.mouselistener.isMouseClicked()) {
            clicks++;
            if (clicks == 2) {
                //easter egg :D
                for (Cloud c : clouds) {
                    c.toggle();
                }
                cticks = 0;
                clicks = 0;
            }
        }
    }

    @Override
    public void render() {
        draw(sky, 0, 0);
        for (Cloud c : clouds) {
            draw(cloudimg[c.type], c.X, c.Y);
        }

        draw(pole, RPG.WINDOW_WIDTH / 2 - 110, poletop);
        draw(pole, RPG.WINDOW_WIDTH / 2 + 110 - poleimg.width, poletop);

        for (Component c : components) {
            c.redraw();
            draw(c, c.getX(), c.getY());
        }

        Dialog d = DialogFactory.getInstance().activeDialog();
        if (d != null) {
            d.draw();
            drawOnBlend(d, d.getX(), d.getY());
        }
    }

    @Override
    public Bitmap getGraphics() {
        return this;
    }
}
